/*
 * Copyright (C), 2014-2015, 杭州小卡科技有限公司
 * Created by ciwei@xiaokakeji.com on 2016/06/05.
 */
package com.micromall.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.micromall.repository.AdminStatisticsMapper;
import com.micromall.repository.entity.ReportInfo;
import com.micromall.utils.ChainMap;
import com.micromall.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ciwei@xiaokakeji.com
 * @date 2016/06/05.
 */
@Service
public class AdminStatisticsService {

	@Resource
	private AdminStatisticsMapper adminStatisticsMapper;

	public static void main(String[] args) {
		Date current = DateUtils.cleanHHMMSS(new Date());
		Date startDate = DateUtils.addMonths(current, -1);
		Date endDate = current;

		// 生成开始/截止时间之间每隔时间点的数据（默认数据都为0）
		Map<String, ReportInfo> memberReportMap = new LinkedHashMap<>();
		for (Date date : getDates(startDate, endDate)) {
			String dateKey = DateUtils.format(date, DateUtils.YYYY_MM_DD);
			memberReportMap.put(dateKey, new ReportInfo(dateKey, "0"));
		}

		System.out.println(memberReportMap.values());

	}

	/**
	 * <pre>
	 * 生成2个日期之间的时间间隔日期
	 *
	 * @param start
	 * @param end
	 * @return List<Date>
	 * @date 2015年5月28日 下午2:14:06
	 */
	private static List<Date> getDates(Date start, Date end) {
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		Calendar temp = Calendar.getInstance();
		startCalendar.setTime(start);
		endCalendar.setTime(end);
		temp.setTime(start);

		List<Date> result = new ArrayList<>();
		while (temp.before(endCalendar)) {
			result.add(temp.getTime());
			temp.add(Calendar.DATE, 1);
		}
		result.add(end);
		return result;
	}

	@Transactional(readOnly = true)
	public Map<String, Object> statistics() {
		Map<String, Object> data = Maps.newHashMap();
		data.put("member_count", adminStatisticsMapper.getMemberTotalNumber());
		data.put("goods_count", adminStatisticsMapper.getGoodsTotalNumber());
		data.put("order_count", adminStatisticsMapper.getOrderTotalNumber());
		data.put("total_sales", adminStatisticsMapper.getTotalSales());
		data.put("total_withdraw", adminStatisticsMapper.getTotalWithdraw());

		Date current = DateUtils.cleanHHMMSS(new Date());
		Date startDate = DateUtils.addMonths(current, -1);
		Date endDate = current;

		List<Object> MEMBER_REPORT_LABEL_LIST = Lists.newArrayList();
		List<Object> MEMBER_REPORT_COUNT_LIST = Lists.newArrayList();
		List<Object> ORDER_REPORT_LABEL_LIST = Lists.newArrayList();
		List<Object> ORDER_REPORT_COUNT_LIST = Lists.newArrayList();
		for (Date date : getDates(startDate, endDate)) {
			String dateKey = DateUtils.format(date, DateUtils.YYYY_MM_DD);
			MEMBER_REPORT_LABEL_LIST.add(dateKey);
			MEMBER_REPORT_COUNT_LIST.add("0");
			ORDER_REPORT_LABEL_LIST.add(dateKey);
			ORDER_REPORT_COUNT_LIST.add("0");
			//			memberReportMap.put(dateKey, new ReportInfo(dateKey, "0"));
			//			orderReportMap.put(dateKey, new ReportInfo(dateKey, "0"));
		}

		List<ReportInfo> member_report = adminStatisticsMapper.getMemberReport(startDate, endDate);
		List<ReportInfo> order_report = adminStatisticsMapper.getOrderReport(startDate, endDate);

		for (ReportInfo report : member_report) {
			//			memberReportMap.get(report.getTime()).setCount(report.getCount());
			//			MEMBER_REPORT_LABEL_LIST.add(report.getTime());
			//			MEMBER_REPORT_COUNT_LIST.add(report.getCount());
			int index = MEMBER_REPORT_LABEL_LIST.indexOf(report.getTime());
			MEMBER_REPORT_COUNT_LIST.set(index, report.getCount());
		}

		for (ReportInfo report : order_report) {
			//			orderReportMap.get(report.getTime()).setCount(report.getCount());
			//			ORDER_REPORT_LABEL_LIST.add(report.getTime());
			//			ORDER_REPORT_COUNT_LIST.add(report.getCount());
			int index = ORDER_REPORT_LABEL_LIST.indexOf(report.getTime());
			ORDER_REPORT_COUNT_LIST.set(index, report.getCount());
		}

		data.put("order_report", new ChainMap<>("label", MEMBER_REPORT_LABEL_LIST).append("count", MEMBER_REPORT_COUNT_LIST));
		data.put("member_report", new ChainMap<>("label", ORDER_REPORT_LABEL_LIST).append("count", ORDER_REPORT_COUNT_LIST));

		/*// 生成开始/截止时间之间每隔时间点的数据（默认数据都为0）
		Map<String, ReportInfo> memberReportMap = new LinkedHashMap<>();
		Map<String, ReportInfo> orderReportMap = new LinkedHashMap<>();
		for (Date date : getDates(startDate, endDate)) {
			String dateKey = DateUtils.format(date, DateUtils.YYYY_MM_DD);
			memberReportMap.put(dateKey, new ReportInfo(dateKey, "0"));
			orderReportMap.put(dateKey, new ReportInfo(dateKey, "0"));
		}

		List<ReportInfo> memberReport = adminStatisticsMapper.getMemberReport(startDate, endDate);
		List<ReportInfo> orderReport = adminStatisticsMapper.getOrderReport(startDate, endDate);

		for (ReportInfo report : memberReport) {
			memberReportMap.get(report.getTime()).setCount(report.getCount());
			//			MEMBER_REPORT_LABEL_LIST.add(report.getTime());
			//			MEMBER_REPORT_COUNT_LIST.add(report.getCount());
		}
		for (ReportInfo report : orderReport) {
			orderReportMap.get(report.getTime()).setCount(report.getCount());
			//			ORDER_REPORT_LABEL_LIST.add(report.getTime());
			//			ORDER_REPORT_COUNT_LIST.add(report.getCount());
		}

		List<Object> MEMBER_REPORT_LABEL_LIST = Lists.newArrayList();
		List<Object> MEMBER_REPORT_COUNT_LIST = Lists.newArrayList();
		List<Object> ORDER_REPORT_LABEL_LIST = Lists.newArrayList();
		List<Object> ORDER_REPORT_COUNT_LIST = Lists.newArrayList();

		data.put("order_report", orderReportMap.values());
		data.put("member_report", memberReportMap.values());*/
		return data;
	}

}
